package com.ll.daipao.service;

import com.alibaba.fastjson.JSONObject;
import com.ll.daipao.dao.*;
import com.ll.daipao.vo.*;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by rockyll on 2017/10/13.
 */
@Service
public class OrderService {


    @Autowired
    private WxMpService wxMpService;

    @Autowired
    TOrderMapper tOrderMapper;

    @Autowired
    TAccountMapper tAccountMapper;

    @Autowired
    TSchoolMapper tSchoolMapper;

    @Autowired
    TOrderTypeMapper tOrderTypeMapper;
    @Autowired
    TOrderDelMapper tOrderDelMapper;


    public void  index(HttpServletRequest request, String opendid) throws WxErrorException {
        Map tAccount = tAccountMapper.selectUserInfoByOpenId(opendid);
        if(tAccount==null){
            WxMpUser wxMpUser = wxMpService.getUserService().userInfo(opendid);
            TAccount ta = new TAccount();
            ta.setNickName(wxMpUser.getNickname());
            ta.setCreateTime(new Date());
            ta.setCreateBy("wx");
            ta.setOpenid(wxMpUser.getOpenId());
            ta.setHendimgurl(wxMpUser.getHeadImgUrl());
            tAccountMapper.insertSelective(ta);
        }
        List<TOrderType> tOrderTypes = selectOrderType();
        request.setAttribute("typelist",tOrderTypes);
        request.setAttribute("account",tAccount);
    }

    public List<Map<String,Object>> selectAll(Map<String ,Object> map){
            return tOrderMapper.selectAll(map);
    }

    /**
     * 通过openid 查询所有的发单
     * @param openId  openid
     * @return
     */
    public List<Map<String,Object>>   mineObtainOrderlist(String openId){
        return tOrderMapper.selectAllHeOrderByOpenId(openId);
    }

    //发单
    public List<Map<String,Object>> selectAllHeOrderByMap(Map<String,Object> map){
        return tOrderMapper.selectAllHeOrderByMap(map);
    }
    //接单
    public List<Map<String,Object>> selectAllReOrderByMap(Map<String,Object> map){
        return tOrderMapper.selectAllReOrderByMap(map);
    }


    public JSONObject myhome(String openId){
        JSONObject res = new JSONObject();
        TAccount account = tAccountMapper.selectAccountByOpenId(openId);
        if(account==null){
            return res;
        }
        String id = String.valueOf(account.getId());
        Integer schoolId = account.getSchoolId();
        if(schoolId!=null){
            TSchool tSchool = tSchoolMapper.selectByPrimaryKey(schoolId);
            res.put("tSchool",tSchool);
        }
        Integer  sendcount = tOrderMapper.selectAllOrderBysend(id);
        res.put("sendcount",sendcount);
        Integer sums = tOrderMapper.selectAllOrderAmountByre(id);
        res.put("sums",sums);
        int accepts = tOrderMapper.selectAllReOrderByre(id);
        res.put("accepts",accepts);
        res.put("account",account);
        return res;
    }


    public List<TOrderType> selectOrderType(){
        return tOrderTypeMapper.selectOrderType();
    }


    public JSONObject selectDetail(Integer id ,String openId){
        JSONObject res = new JSONObject();
        TAccount tAccount = tAccountMapper.selectAccountByOpenId(openId);
        if(tAccount==null){
            return res;
        }
        Integer reid = tAccount.getId();
        TOrder tOrder = tOrderMapper.selectByPrimaryKey(id);
        res.put("tAccount",tAccount);
        res.put("order",tOrder);
        return res;
    }

    public List<Map<String, Object>> selectAllMoney(String openid){
        return tOrderMapper.selectAllHeOrderByOpenId(openid);
    }

    @Transactional(rollbackFor = Exception.class)
    public JSONObject saveOrder(String openId , TOrder tOrder) throws Exception{
        JSONObject res = new JSONObject();
        TAccount tAccount = tAccountMapper.selectAccountByOpenId(openId);
        if(tAccount==null){
            res.put("suc",false);
            res.put("msg","没找到相关登录人信息");
            return res;
        }
        String orderType = tOrder.getOrderType();
        TOrderType tOrderType = tOrderTypeMapper.selectByPrimaryKey(Integer.parseInt(orderType));

        if(tOrderType==null){
            res.put("suc",false);
            res.put("msg","未找到相关订单的类型");
            return res;
        }
        tOrder.setPayStatus("2");
        tOrder.setOrderStatus("1");
        tOrder.setHeAccountId(tAccount.getId());
        tOrder.setHeSchoolId(tAccount.getSchoolId());
        tOrderMapper.insertSelective(tOrder);
        res.put("suc",true);
        res.put("id",tOrder.getId());
        res.put("payamount",tOrderType.getPayAmount());
        return res;

    }

    public  int paySuccessUpdateOrder(String id){
        TOrder tOrder = tOrderMapper.selectByPrimaryKey(Integer.parseInt(id));
        tOrder.setPayStatus("1");
        int i = tOrderMapper.updateByPrimaryKeySelective(tOrder);
      return i;
    }


    @Transactional(rollbackFor = Exception.class)
    public JSONObject reOrder(String openId , String id)throws Exception{
        JSONObject res = new JSONObject();
        TAccount tAccount = tAccountMapper.selectAccountByOpenId(openId);
        if(tAccount==null){
            res.put("suc",false);
            res.put("msg","缺失重要数据！");
            return res;
        }
        TOrder tOrder = tOrderMapper.selectByPrimaryKey(Integer.parseInt(id));

        if(tOrder==null){
            res.put("suc",false);
            res.put("msg","找不到相关订单！");
            return res;
        }
        if(tOrder.getReAccountId()==tAccount.getId()){
            res.put("suc",true);
            return res;
        }
        if(!"1".equals(tOrder.getOrderStatus())){
            res.put("suc",false);
            res.put("msg","订单已被接单，请加快手速");
            return res;
        }
        tOrder.setOrderStatus("2");

        tOrder.setReAccountId(tAccount.getId());
        tOrder.setReOrderTime(new Date());
        tOrderMapper.updateByPrimaryKeySelective(tOrder);
        res.put("suc",true);
        return res;
    }

    @Transactional(rollbackFor = Exception.class)
    public JSONObject confirmOrder(String openId , String id)throws Exception{
        JSONObject res = new JSONObject();
        TAccount tAccount = tAccountMapper.selectAccountByOpenId(openId);
        if(tAccount==null){
            res.put("suc",false);
            res.put("msg","缺失重要数据！");
            return res;
        }
        TOrder tOrder = tOrderMapper.selectByPrimaryKey(Integer.parseInt(id));

        if(tOrder==null){
            res.put("suc",false);
            res.put("msg","找不到相关订单！");
            return res;
        }
        if(!"2".equals(tOrder.getOrderStatus())){
            res.put("suc",false);
            res.put("msg","请确认订单状态");
            return res;
        }
        tOrder.setOrderStatus("3");
        tOrder.setCloseTime(new Date());
        tOrderMapper.updateByPrimaryKeySelective(tOrder);
        res.put("suc",true);
        return res;
    }

    public int selectMySendOrdercount(String openId){
        List<Map<String, Object>> maps = tOrderMapper.selectAllHeOrderByOpenId(openId);
        return  maps.size();
    }

    public JSONObject saveSchool(String openId ,String id){
        JSONObject res = new JSONObject();
        res.put("suc",false);
        TSchool tSchool = tSchoolMapper.selectByPrimaryKey(Integer.parseInt(id));
        if(tSchool==null){
            res.put("msg","学校数据异常！");
            return res;
        }
        TAccount tAccount = tAccountMapper.selectAccountByOpenId(openId);
        tAccount.setSchoolId(Integer.parseInt(id));
        tAccountMapper.updateByPrimaryKeySelective(tAccount);
        res.put("suc",true);
        return res;
    }

    public  JSONObject cancelOrder(String openid , String id){
        JSONObject res = new JSONObject();
        TAccount tAccount = tAccountMapper.selectAccountByOpenId(openid);
        if(tAccount==null){
            res.put("suc",false);
            res.put("msg","无当前用户星系");
            return res;
        }
        TOrder tOrder = tOrderMapper.selectByPrimaryKey(Integer.parseInt(id));
        if(tOrder==null){
            res.put("suc",false);
            res.put("msg","无订单操作权限");
            return res;
        }
        if(tAccount.getId()==tOrder.getReAccountId()){
            res.put("suc",true);
            tOrder.setOrderStatus("4");
            TOrderDel tOrderDel =new TOrderDel();
             BeanUtils.copyProperties(tOrder,tOrderDel);
            tOrderDelMapper.insertSelective(tOrderDel);
            tOrder.setReAccountId(null);
            tOrder.setReOrderTime(null);
            tOrder.setOrderStatus("1");
            tOrderMapper.updateByPrimaryKeySelective(tOrder);
        }else {
            res.put("suc",false);
            res.put("msg","无订单操作权限");
        }
        return res;
    }
}
